/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb.login;

import applicationexcepcion.AccountBannatoException;
import applicationexcepcion.AccountBloccatoException;
import applicationexcepcion.AccountLoginException;
import common.Stato;
import ejb.cliente.ClienteEJB;
import ejb.cliente.ClienteEJBLocal;
import ejb.common.AccountManagerLocal;
import ejb.common.TripleDESLocal;
import ejb.moderatore.ModeratoreEJBLocal;
import ejb.supervisore.SupervisoreEJBLocal;
import entity.Account;
import entity.Cliente;
import entity.Moderatore;
import entity.Persona;
import entity.Supervisore;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 *
 * @author Antonio
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class Login implements LoginLocal {

    @EJB
    private SupervisoreEJBLocal supervisoreEJB;
    @EJB
    private ModeratoreEJBLocal moderatoreEJB;
    @EJB
    private ClienteEJBLocal clienteEJB;
    @EJB
    private TripleDESLocal tripleDES;
    @EJB
    private AccountManagerLocal accountManager;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    /**
     *
     * @param email
     * @param password
     * @throws AccountLoginException
     */
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Persona accesso(String email, String password) throws AccountLoginException, AccountBannatoException, AccountBloccatoException {

        Account account = new Account();
        account.setEmail(email);

        // controllo se l'account esiste
        if (!accountManager.isRegistrato(account)) {

            throw new AccountLoginException();
        }
        account = accountManager.ottieniAccount(account);

        // controllo delle password
        try {

            String pwsCry = tripleDES.encrypt(password);
            if (!pwsCry.equals(account.getPassword())) {

                throw new AccountLoginException();
            }

        } catch (Exception ex) {

            throw new AccountLoginException();
        }
        // verifico lo stato dell'account
        return verificaAccount(account);
    }// accesso

    private Persona verificaAccount(Account account) throws AccountBannatoException, AccountBloccatoException, AccountLoginException {

        if (account == null) {

            throw new AccountLoginException();
        }

        switch (account.getStato()) {

            case Bloccato:
                throw new AccountBloccatoException();
            case Bannato:
                throw new AccountBannatoException();
            case Attivo:
                return accountManager.cercaPersona(account.getEmail());

            default:
                return null;
        }
    }// verificaAccount
}
